﻿google.load('feeds', '1');
var feedList = new Array();

function resetfeeds() {
    $("#results").fadeOut('slow', function () {
        $('#feedQuery').val("").focus();
        $("#results").empty();
    });
    return false;
}

function samplefeeds() {
    $('#feedQuery').val($(this).attr("rel"));
    findfeeds();
    return false;
}

function findfeeds() {
    var q = $.trim($('#feedQuery').val());
    if (q == "") {
        resetfeeds();
        return false;
    }

    $('#pleasewait').show();

    google.feeds.findFeeds(q, function (result) {
        if (!result.error) {
            var html = '';
            for (var i = 0; i < result.entries.length; i++) {
                var entry = result.entries[i];
                feedList[i] = entry.url;
                var count = i + 1;
                html += '<div id="feed-' + i + '">';
                html += '	<h3><img src="//s2.googleusercontent.com/s2/favicons?domain=' + entry.link + '"/> <a target="_blank" href="' + entry.link + '">' + removeHTMLTags(entry.title) + '</a></h3>';
                html += '	<p class="snippet">' + removeHTMLTags(entry.contentSnippet) + '</p>';
                html += '	<p class="snippet"><i>' + entry.contentSnippet + '</i></p>';
                html += '	<p class="feedURL">';
                html += '<a href="' + entry.url + '" target="_blank">RSS Feed</a> &sdot; ';
                html += ' <span class="showhide" rel="' + i + '">Preview Feed</span></p>';
                html += ' <div id="feedcontent-' + i + '"></div>';
                html += '</div>';
            }

            $("#results").fadeOut('slow', function () {
                $('html, body').animate({ scrollTop: 0 }, 'slow');
                $("#results").empty();
                $("#results").append(html);
                $("#results").show();
            });
        }
        $('#pleasewait').hide();
    });
    return false;
}


function removeHTMLTags(html) {
    var cleanHTML =
			html.replace(/&(lt|gt);/g, function (strMatch, p1) {
			    return (p1 == "lt") ? "<" : ">";
			});
    return cleanHTML.replace(/<\/?[^>]+(>|$)/g, "");
}

function getfeed(i) {
    var divcount = 'div#feedcontent-' + i + " > div";
    if ($(divcount).length > 0) {
        $('div#feedcontent-' + i).animate({ "height": "toggle" }, { duration: 500 });
        if ($('div#feed-' + i).find("span.showhide").text() == "Preview Feed")
            $('div#feed-' + i).find("span.showhide").text("Hide Feed Preview");
        else
            $('div#feed-' + i).find("span.showhide").text("Preview Feed");
        return;
    }
    var feedURL = feedList[i];
    var feed = new google.feeds.Feed(feedURL);
    feed.setNumEntries(20)
    var html = '';

    feed.load(function (result) {
        if (!result.error) {
            for (var c = 0; c < result.feed.entries.length; c++) {
                var entry = result.feed.entries[c];
                var ct = c + 1;
                html += '<div id="item-' + c + '"><h4>' + ct + '. <a target="_blank" href="' + entry.link + '">';
                html += '	<p class="snippet"><i>' + entry.content + '</i></p>';
                html += entry.title + '</a> ' + entry.publishedDate.substring(5, 16) + '</h4><!-- <small>' + entry.contentSnippet + '</small> --></div>';
            }
            $('div#feedcontent-' + i).addClass("feedItems").append(html);
            $('div#feed-' + i).find("span.showhide").text("Hide Feed Preview");
        }
    });
}

$(document).ready(function () {

    $('#feedQuery').keypress(function (e) {
        if (e.which == 13) {
            jQuery(this).blur();
            jQuery('#searchBtn').focus().click();
            return false;
        }
    });

    $("#searchBtn").click(findfeeds);
    $("h1").click(resetfeeds);
    $(".examples").click(samplefeeds);

    $("span.showhide").live("click", function (event) {
        getfeed($(this).attr("rel"));
    });

    $('#feedQuery').focus();

});


var timer;
$('#feedQuery').keyup(function () {
    timer && clearTimeout(timer);
    timer = setTimeout(findfeeds, 400);
});